package cn.itsource.ymcc.feign;


import cn.itsource.result.JSONResult;
import cn.itsource.ymcc.doc.CourseDoc;
import feign.hystrix.FallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@FeignClient(value = "service-search",fallbackFactory = SearchFeignClientFallbackFactory.class)
public interface SearchFeignClient {

    @PostMapping("/course/save")
    JSONResult saveCourse(@RequestBody CourseDoc doc);

    @PostMapping("/course/delete")
    JSONResult deleteCourse(@RequestBody Long id);


}

@Component
class SearchFeignClientFallbackFactory implements FallbackFactory<SearchFeignClient> {
    @Override
    public SearchFeignClient create(Throwable throwable) {
        return new SearchFeignClient() {
            @Override
            public JSONResult saveCourse(CourseDoc doc) {
                throwable.printStackTrace();
                return JSONResult.error("发布课程失败！");
            }

            @Override
            public JSONResult deleteCourse(Long id) {
                throwable.printStackTrace();
                return JSONResult.error("下架课程失败！");
            }
        };
    }
}

